package com.zking.filter;

import com.zking.config.config;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class DebugFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession(false);

        boolean loggedIn = session != null && session.getAttribute("user")!=null;
        if(config.is_debug && !loggedIn){
            String uri = req.getRequestURI();
            if(uri.contains("shopcar") || uri.contains("ShopCar"))
            {
                resp.sendRedirect("shopping.jsp");
                return;
            }else if(uri.contains("balance")){
                resp.sendRedirect("shopping-result.jsp");
                return;
            }else if(uri.contains("ViewList")){
                resp.sendRedirect("guestbook.jsp");
                return;
            }
        }
        chain.doFilter(request, response);
    }
}
